<template>
  <div id="mySpace">
    <p>正在跳转，请稍后....</p>
  </div>
</template>

<script lang="ts" setup>
import { listSpaceVoPageUsingPost } from '@/api/spaceController'
import { useLoginUserStore } from '@/stores/user'
import { message } from 'ant-design-vue'
import { onMounted } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const loginUserStore = useLoginUserStore()

//检查用户是否有个人空间
const checkUserSpace = async () => {
  const loginUser = loginUserStore.loginUser
  if (!loginUser?.id) {
    router.push('/user/login')
    return
  }
  //获取用户空间信息
  const res = await listSpaceVoPageUsingPost({
    userId: loginUser.id,
    current: 1,
    pageSize: 1,
  })
  if (res.data.code === 0) {
    if (res.data.data?.records && res.data.data.records.length > 0) {
      //有个人空间，跳转到个人空间页面
      const space = res.data.data.records[0]
      router.replace(`/space/${space.id}`)
    } else {
      //没有个人空间，跳转到创建空间页面
      router.replace('/add_space')
      message.warn('请先创建空间')
    }
  } else {
    message.error('获取空间信息失败' + res.data.message)
  }
}

onMounted(() => {
  checkUserSpace()
})
</script>

<style lang="scss" scoped></style>
